package main

import (
	"natsmicro/conf"
	"sync"

	"github.com/gogf/gf/frame/g"
	"github.com/nats-io/stan.go"
)

var donewg sync.WaitGroup

func main() {

	// logger := glog.New()
	// logger.SetConfigWithMap(g.Map{
	// 	"path":     "./",
	// 	"level":    "all",
	// 	"stdout":   false,
	// 	"StStatus": 0,
	// })

	//stan.Connect(clusterID, clientID, ops ...Option)
	ns, err := stan.Connect("test-cluster", "myI3D", stan.NatsURL(conf.NatsUrl))

	if err != nil {
		println(err.Error())
	}
	// Simple Synchronous Publisher
	// does not return until an ack has been received from NATS Streaming

	NN := 10
	var subs []stan.Subscription
	for i := 0; i < NN; i++ {
		// Simple Async Subscriber
		j := i
		sub1, _ := ns.Subscribe("foo", func(m *stan.Msg) {
			//log.Printf("sub1: %s\n", string(m.Data))
			g.Log().Info(m.Data)
			g.Log().Info(j)

		})
		subs = append(subs, sub1)

	}
	select {}
	// Unsubscribe
	for i := 0; i < NN; i++ {
		subs[i].Unsubscribe()
	}
	// Close connection
	ns.Close()
}
